package cn.yunhe.crm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.yunhe.crm.annotation.SystemLog;
import cn.yunhe.crm.entity.Diary;
import cn.yunhe.crm.entity.Organization;
import cn.yunhe.crm.service.DiaryService;
import cn.yunhe.crm.service.OrganizationService;
import net.sf.json.JSONArray;

@Controller
public class DiaryController {
	@Autowired
	@Qualifier("DiaryService")
	private DiaryService diaryservice;
	@Autowired
	@Qualifier("OrganizationService")
	private OrganizationService organizationservice;
	
	public void setDiaryservice(DiaryService diaryservice) {
		this.diaryservice = diaryservice;
	}
	
	public void setOrganizationservice(OrganizationService organizationservice) {
		this.organizationservice = organizationservice;
	}

	//显示列表信息
	@RequestMapping("logmanage.do")
	public String DiaryManage(HttpSession session){
		List<Diary> diarylist=diaryservice.findAllDiary();
		session.setAttribute("diarylist",diarylist);
		return "logmanage";
	}
	//查看日志详情
	@RequestMapping("log_detail.do")
	public String log_detail(HttpSession session,Integer id){
		Diary diary=diaryservice.findDiaryById(id);
		session.setAttribute("diary", diary);
		return "log_detail";
	} 
	
	/**
	 * 日志添加页面的加载
	 * @return
	 */
	@RequestMapping("log_add.do")
	public String log_add(){
		return "log_add";
	}
	/**
	 * 添加日志
	 * @return
	 */
	@SystemLog(des="添加售后日志")
	@RequestMapping("diary_add.do")
	public String diaryadd(Diary diary){
		int add=diaryservice.insertDiary (diary);
		return "redirect:/logmanage.do";
	}
	
	
	@ResponseBody
	@RequestMapping(value="/checkoid.do",produces ="application/json;charset=UTF-8")  
	public JSONArray checkoid() {
		List<Organization> list=organizationservice.getOrgList();
		//Map map=new HashMap();
		List list2=new ArrayList();
		if (list.size()>0) {
			for (Organization organization : list) {
				Map map=new HashMap();
				map.put("oid", organization.getId());
				map.put("name", organization.getName());
				map.put("cname", organization.getContact().getName());
				map.put("cename", organization.getContact().getEname());
				map.put("type", organization.getType());
				map.put("ctel", organization.getContact().getTel());
				map.put("vip", organization.getVip());
				map.put("province", organization.getProvince());
				list2.add(map);
			}
		}
		return JSONArray.fromObject(list2);
	}
	
}








